package com.bingbing.bingbingmall.order.web;

import com.alibaba.fastjson.TypeReference;
import com.bingbing.bingbingmall.order.feign.MemberFeignService;
import com.bingbing.bingbingmall.order.feign.ProductFeignService;
import com.bingbing.bingbingmall.order.interceptor.LoginUserInterceptor;
import com.bingbing.bingbingmall.order.service.OrderItemService;
import com.bingbing.bingbingmall.order.service.OrderService;
import com.bingbing.bingbingmall.order.vo.MemberAddressVo;
import com.bingbing.bingbingmall.order.vo.SkuInfo;
import com.bingbing.bingbingmall.order.vo.SpuInfoVo;
import com.bingbing.common.constant.AuthServerConstant;
import com.bingbing.common.utils.R;
import com.bingbing.common.vo.MemberRespVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class PayWebController {

    @Autowired
    OrderService orderService;

    @Autowired
    OrderItemService orderItemService;

    @Autowired
    MemberFeignService memberFeignService;

    @Autowired
    ProductFeignService productFeignService;


    @PostMapping("/payed/Order")
    public String handleAlipayed(String password, String orderSn, Long skuId, HttpSession session, RedirectAttributes redirectAttributes){

        MemberRespVo data =(MemberRespVo) session.getAttribute(AuthServerConstant.LOGIN_USER);
        //1、获取到数据库的password  $2a$10$2xOI1.2DTQxpWeWd3Rk0qOVPTpauodlYkafTjNb4LOMuS1zBEZc5K
        String passwordDb = data.getPassword();
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        //2、密码匹配
        System.out.println("_____________________________________");
        System.out.println(password);
        System.out.println(passwordDb);
        boolean matches = passwordEncoder.matches(password, passwordDb);


        if(matches){
            System.out.println("签名验证成功...");
            String result = orderService.handlePayResult(orderSn);

            if(result == "success"){
                MemberRespVo memberRespVo = LoginUserInterceptor.loginUser.get();
                List<MemberAddressVo> address = memberFeignService.getAddress(memberRespVo.getId());
                MemberAddressVo memberAddressVo = address.get(0);
                String res = orderService.setOrder(memberAddressVo, orderSn);

                System.out.println(skuId);
                R r = productFeignService.getSpuInfoBySkuId((skuId));
                SpuInfoVo spuInfoVo = r.getData(new TypeReference<SpuInfoVo>() {});

                R r1 = productFeignService.getSkuInfoBySkuId((skuId));
                System.out.println(r1);
                SkuInfo skuInfo = r1.getData(new TypeReference<SkuInfo>() {});
                System.out.println(skuInfo);
                String res2 = orderItemService.setOrderItem(spuInfoVo, skuInfo, orderSn);
                System.out.println(memberAddressVo);

                System.out.println(spuInfoVo);
                if (res == "success" && res2 == "success") {
                    return "redirect:http://localhost:8000/memberOrder.html";
                }
            } else if (result == "error"){
                return "redirect:http://localhost:8000/memberOrder.html?message=0";
            }
            System.out.println("签名验证失败...");
            return "redirect:http://localhost:25000/toFail.html";
        }else {
            System.out.println("签名验证失败...");
            return "redirect:http://localhost:25000/toFail.html";
        }

    }
}
